package kuzmenkov.oip.controller.applet;

import java.applet.Applet;
import java.net.URL;
import java.text.NumberFormat;
import java.util.List;
import java.util.logging.Logger;

/**
 * Class is responsible for to create all parameter components
 * 
 * @author Michael Kuzmenkov
 * 
 */
public class ParamUICFactoryImpl implements ParamUICFactory {
	private static Logger log = Logger.getLogger(ParamUICFactory.class
			.getName());

	/**
	 * A list of appropriate images
	 */
	private List images;
	private Applet applet;
	private ISelectableUIC selectableUIC;
	private IPolygonSupportedUIC polygonSupportedUIC;

	public void setSelectableUIC(ISelectableUIC selectableUIC) {
		this.selectableUIC = selectableUIC;
	}

	public ParamUICFactoryImpl(Applet applet, List images) {
		this.images = images;
		this.applet = applet;
		
	}

	/**
	 * Creates a particular component on the basis of the given parameter
	 * 
	 * @param parameter
	 * @return
	 * @throws Exception 
	 */
	public ParamUIC createParamComponent(Param param) {
		ParamUIC c = null;
		I18nTool itool = I18nTool.getInstance();

		String type = param.getType();
		if (Param.BOOLEAN_TYPE.equals(type)) {
			c = createBooleanComponent(param);
		} else if (Param.INTEGER_TYPE.equals(type)) {
			c = createIntegerComponent(param);
		} else if (Param.FLOAT_TYPE.equals(type)) {
			c = createFloatComponent(param);
		} else if (Param.STRING_TYPE.equals(type)) {
			c = createStringComponent(param);			
		} else if (Param.IMAGE_TYPE.equals(type)) {
			c = createImageComponent(applet.getCodeBase(), param, images);
			selectableUIC = (ISelectableUIC) c;
			polygonSupportedUIC = (IPolygonSupportedUIC) c;
		} else if (Param.SELECTION_TYPE.equals(type)){
			if (selectableUIC==null)
				throw new IllegalStateException(itool.getMessage("ParamUICFactoryImpl.selectableComponentNotExist"));
			c = createSelectionComponent(param, selectableUIC);
			selectableUIC = null;
		} else if (Param.POLYGONS_TYPE.equals(type)){
			if (polygonSupportedUIC==null)
				throw new IllegalStateException(itool.getMessage("ParamUICFactoryImpl.polygonSupportedComponentNotExist"));
			c = createPolygonsComponent(param, polygonSupportedUIC);
			polygonSupportedUIC = null;
		} else {
			String err = itool.getMessage(
					"ParamUICFactoryImpl.unknownType",
					new Object[] { type });
			log.severe(err);
			throw new IllegalArgumentException();
		}

		return c;
	}


	private ParamUIC createBooleanComponent(Param param) {
		BooleanUIC c = new BooleanUIC(param);
		return c;
	}

	private ImageUIC createImageComponent(URL codebase, Param param,
			List images2) {
		ImageUIC c = new ImageUIC(param, codebase, images2, 250, 250);
		
		return c;
	}

	private FieldUIC createIntegerComponent(Param param) {
		NumberFormat format = NumberFormat.getIntegerInstance();
		FieldUIC c = new FieldUIC(param, format, 10);
		String defVal = param.getDefaultValue();
		if (defVal != null)
			c.setValue(new Integer(defVal));
		return c;
	}

	private FieldUIC createFloatComponent(Param param) {
		I18nTool itool = I18nTool.getInstance();
		NumberFormat format = NumberFormat.getNumberInstance(itool.getLocale());
		format.setMaximumFractionDigits(6);
		format.setMinimumFractionDigits(1);
		

		FieldUIC c = new FieldUIC(param, format, 10);
		String defVal = param.getDefaultValue();
		if (defVal != null)
			c.setValue(new Float(defVal));
		return c;
	}
	
	private FieldUIC createStringComponent(Param param) {
		FieldUIC c = new FieldUIC(param, null, 15);
		String defVal = param.getDefaultValue();
		if (defVal != null)
			c.setValue(defVal);
		return c;
	}
	
	private SelectionUIC createSelectionComponent (Param param, ISelectableUIC selectableUIC){
		SelectionUIC spc = new SelectionUIC(param,selectableUIC);
		selectableUIC.setSelectable(true);
		return spc;
	}
	
	private ParamUIC createPolygonsComponent(Param param,
			IPolygonSupportedUIC polygonSupportedUIC) {
		PolygonsUIC c = new PolygonsUIC(param,polygonSupportedUIC);
		polygonSupportedUIC.setSupport(true);
		return c;
	}



}
